package net.lcheng.sellerCenter.controller;


import net.lcheng.commons.utils.ParseMD5;
import net.lcheng.model.SysUsers;
import net.lcheng.model.User;
import net.lcheng.sellerCenter.utils.ControllerUtils;
import net.lcheng.sellerCenter.vo.PasswordModel;
import net.lcheng.service.SysPermissionsService;
import net.lcheng.service.SysUsersService;
import net.lcheng.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

@Controller
public class HomeController {

	@Autowired
	private SysUsersService usersService;
	@Autowired
	private SysPermissionsService permissionsService;
	@Autowired
	private UserService userService;
	@RequestMapping("/main")
//	@RequiresRoles("admin")
//	@RequiresPermissions("user:create")
	public String main(Model model) throws Exception {
		Subject currentUser= SecurityUtils.getSubject();
			String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
			//获取用户信息
			User user = userService.getUserByPhone(userPhoneNum);
			model.addAttribute("user",user);
		return "main";
	}

	/**权限不足错误页面*/
	@RequestMapping("/unauthorized")
	public String unauthorized(Model model){
		Subject currentUser = SecurityUtils.getSubject();//获取当前用户
		if(currentUser.isAuthenticated()) {
			model.addAttribute("userName",currentUser.getPrincipals());
		}
		return "unauthorized";
	}

	/**错误页面*/
	@RequestMapping("/error")
	public String error(Model model){
		Subject currentUser = SecurityUtils.getSubject();//获取当前用户
		if(currentUser.isAuthenticated()) {
			model.addAttribute("userName",currentUser.getPrincipals());
		}
		return "error";
	}

//	@RequestMapping(value = "/change_pwd",method = RequestMethod.GET)
//	public String changePwd(Model model){
//		model.addAttribute("PasswordModel",new PasswordModel());
//		ControllerUtils.common(model,permissionsService,"");
//
//		return "change_pwd";
//	}
//	@RequestMapping(value = "/change_pwd",method = RequestMethod.POST)
//	public String changePwdPost(Model model,@Valid @ModelAttribute("PasswordModel") PasswordModel user, BindingResult result){
//		Subject currentUser = SecurityUtils.getSubject();//获取当前用户
//		ControllerUtils.common(model,permissionsService,"");
//
//		if (result.hasErrors()) {
//			return "change_pwd";
//		}
//
//		SysUsers sysUser = usersService.getUserByUsername(currentUser.getPrincipals().toString());
//		if(sysUser!=null) {
//			if(!sysUser.getPassword().equals(ParseMD5.parseStrToMd5L32(user.getOldPwd()))){
//				model.addAttribute("error", "原密码错误");
//				return "change_pwd";
//			}
//			if(!user.getNewPwd().equals(user.getRePwd())){
//				model.addAttribute("error", "二次密码不一致");
//				return "change_pwd";
//			}
//
//			if(ParseMD5.parseStrToMd5L32(user.getNewPwd()).equals(ParseMD5.parseStrToMd5L32(user.getOldPwd()))){
//				model.addAttribute("error", "新密码和原密码一样");
//				return "change_pwd";
//			}
//
//			usersService.changePassword(sysUser.getId(), ParseMD5.parseStrToMd5L32(user.getNewPwd()));
//			model.addAttribute("error", "密码修改成功,请使用新密码重新登录!");
//		}else{
//			model.addAttribute("error", "用户不存在");
//		}
//
//		return "change_pwd";
//	}

}
